package com.rfsp.common.base;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

//@Service
public abstract class BaseService<T extends BasePO> implements IBaseService<T>{

	public Logger log = LoggerFactory.getLogger(BaseService.class);

	@Autowired
	protected JdbcTemplate jdbc;

	@Autowired
	protected BaseMapper<T> mapper;

	public int save(T t) {
		return mapper.insertSelective(t);
	}
	public int saveAll(List<T> recordList) {
		return mapper.insertList(recordList);
	}
	public int update(T t) {
		return mapper.updateByPrimaryKeySelective(t);

	}
	public int saveOrUpdate(T t) {
		if(t.getId() == null || t.getId() ==0) {
			return save(t);
		}else {
			return update(t);
		}

	}

	public int remove(Long id) {
		if(id==null)return 0;
		return mapper.deleteByPrimaryKey(id);
	}

	public int removeAll(String ids) {
		return mapper.deleteByIds(ids);
	}

	public T get(Long id) {
		if(id==null)return null;
		return mapper.selectByPrimaryKey(id);

	}

	public T getOne(T t) {
		return mapper.selectOne(t);
	}

	public int getCount(T t) {
		return mapper.selectCount(t);
	}

	public List<T> queryList(T t) {
		if(t==null)return null;
		return mapper.select(t);
	}
	public List<T> queryAll() {
		return mapper.selectAll();
	}

	public List<T> queryListByIds(String ids) {
		if(ids==null)return new ArrayList<T>();
		return mapper.selectByIds(ids);
	}
	
	public String getAID() {
		String str = "1234567890qwertyuiopasdfghjklzxcvbnm";
		Random r = new Random();
		
		StringBuffer sb = new StringBuffer(16);
		
		for(int i=0;i<16;i++) {
			int index = r.nextInt(str.length());
			sb.append(str.charAt(index));
		}
		return sb.toString();
	}
}
